package org.mall.modules.order.controller.admins;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.mall.common.api.vo.CommonResult;
import org.mall.common.api.vo.Result;
import org.mall.common.util.HttpUtil;
import org.mall.convert.order.application.OrderReturnConvert;
import org.mall.modules.order.service.OrderReturnService;
import org.mall.modules.pojo.order.bo.OrderReturnListBO;
import org.mall.modules.pojo.order.dto.OrderReturnQueryDTO;
import org.mall.modules.pojo.order.po.admin.OrderReturnQueryPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 订单退货
 *
 * @author Sin
 * @time 2019-05-06 21:31
 */
@RestController
@RequestMapping("/admins/order_return/")///admins/order_return
@Api("订单退货(admins api)")
public class AdminOrderReturnController {

    @Autowired
    private OrderReturnService orderReturnService;

    @GetMapping("list")
    public Result<?> list(OrderReturnQueryPO queryPO,
                          @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        queryPO.setPageNo(pageNo);
        queryPO.setPageSize(pageSize);
        OrderReturnQueryDTO queryDTO = OrderReturnConvert.INSTANCE.convert(queryPO);
        CommonResult<OrderReturnListBO> boCommonResult = orderReturnService.orderReturnList(queryDTO);
        Page<OrderReturnListBO.OrderReturn> page = new Page<>();
        List<OrderReturnListBO.OrderReturn> data = boCommonResult.getData().getData();
        page.setTotal(boCommonResult.getData().getTotalCount());
        page.setRecords(data);
        return Result.ok(page);
    }

    @PostMapping("agree")
    public CommonResult agree(@RequestParam("id") Integer id) {
        return orderReturnService.orderReturnAgree(id);
    }

    @PostMapping("refuse")
    public CommonResult refuse(@RequestParam("id") Integer id) {
        return orderReturnService.orderReturnRefuse(id);
    }

    @PostMapping("confirm_receipt")
    public CommonResult confirmReceipt(@RequestParam("id") Integer id) {
        return orderReturnService.confirmReceipt(id);
    }

    @PostMapping("confirm_refund")
    public CommonResult confirmRefund(HttpServletRequest request, @RequestParam("id") Integer id) {
        String ip = HttpUtil.getIp(request);
        //确认退款
        return orderReturnService.refund(id, ip);
    }

    /**
     * 获取退单详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public CommonResult detail(@RequestParam("id") Integer id) {

        return orderReturnService.getOrderReturnDetails(id);
    }
}
